export const remUtils = {}
remUtils.resetRem = function(realFS) {
  function setRem() {
    const html = document.documentElement
    html.style.fontSize = realFS + 'px'
  }
  setRem()
  window.onresize = function() {
    setRem()
  }
}
remUtils.changeRatio = function() {
  var ratio = 1
  if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
    ratio = window.outerWidth / window.innerWidth
  }
  if (ratio) {
    ratio = Number(Math.abs(ratio).toFixed(2))
  }
  return ratio
}

const userAgent = navigator.userAgent
// 判断是否Firefox浏览器
if (userAgent.indexOf('Firefox') > -1) {
  const baseSize = 18
  function setRem() {
    const ratio = remUtils.changeRatio()
    const clientWidth = document.documentElement.clientWidth
    const scale = clientWidth * ratio / 1680
    document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
  }
  setRem()
  window.onresize = function() {
    setRem()
  }
}
// 判断是否IE浏览器
if (userAgent.indexOf('IE') > -1) {
  const baseSize = 18
  function setRem() {
    const ratio = remUtils.changeRatio()
    const clientWidth = document.documentElement.clientWidth
    const scale = clientWidth * ratio / 1680
    document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
  }
  setRem()
  window.onresize = function() {
    setRem()
  }
}
// 判断是否chorme浏览器
if (userAgent.indexOf('Chrome') > -1) {
  let baseSize
  // let clientWidthV
  function setRem() {
    const clientWidth = document.documentElement.clientWidth
    baseSize = 18
    const ratio = remUtils.changeRatio()
    // if (clientWidth > 1920) {
    //   baseSize = 20
    //   clientWidthV = 1920
    // } else if (clientWidth > 1680 && clientWidth <= 1920) {
    //   baseSize = 20
    //   clientWidthV = 1920
    // } else if (clientWidth > 1440 && clientWidth <= 1680) {
    //   clientWidthV = 1680
    // } else if (clientWidth > 1366 && clientWidth <= 1440) {
    //   clientWidthV = 1440
    // } else {
    //   clientWidthV = 1366
    // }
    // 当前页面宽度相对于 1920宽的缩放比例，可根据自己需要修改。
    // const scale = clientWidth / clientWidthV
    const scale = clientWidth * ratio / 1920
    // 设置页面根节点字体大小（“Math.min(scale, 2)” 指最高放大比例为2，可根据实际业务需求调整）
    document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
  }
  // 初始化
  setRem()
  window.onresize = function() {
    setRem()
  }
}
